package com.tron.game.configs;

import com.tron.game.jobs.CheckBscJob;
import com.tron.game.jobs.CheckJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * Quartz定时任务配置类
 * @author pan_junbiao
 **/
@Configuration
public class QuartzConfig
{

    @Autowired
    TronConfig tronConfig;
    @Autowired
    BscConfig bscConfig;
    private static String JOB_GROUP_NAME = "PJB_JOBGROUP_NAME";
    private static String TRIGGER_GROUP_NAME = "PJB_TRIGGERGROUP_NAME";

    /**
     * 定时任务1：
     * 同步用户信息Job（任务详情）
     */
    @Bean
    public JobDetail syncUserJobDetail()
    {
        JobDetail jobDetail = JobBuilder.newJob(CheckJob.class)
                .withIdentity("syncUserJobDetail",JOB_GROUP_NAME)
                .storeDurably()
                .build();
        return jobDetail;
    }
    /**
     * 定时任务1：
     * 同步用户信息Job（触发器）
     */
    @Bean
    public Trigger syncUserJobTrigger()
    {
        //每隔5秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(tronConfig.getCronTab());

        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(syncUserJobDetail())//关联上述的JobDetail
                .withIdentity("syncUserJobTrigger",TRIGGER_GROUP_NAME)//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }

    /**
     * 定时任务2：
     * 同步用户信息Job（任务详情）
     */
    @Bean
    public JobDetail BSCUserJobDetail()
    {
        JobDetail jobDetail = JobBuilder.newJob(CheckBscJob.class)
                .withIdentity("BSCUserJobDetail","PJB_JOBGROUP_NAME_BSC")
                .storeDurably()
                .build();
        return jobDetail;
    }


    /**
     * 定时任务2：
     * 同步用户信息Job（触发器）
     */
    @Bean
    public Trigger BSCUserJobTrigger()
    {
        //每隔5秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(bscConfig.getCronTab());

        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(BSCUserJobDetail())//关联上述的JobDetail
                .withIdentity("BSCUserJobTrigger","PJB_JOBGROUP_NAME_BSC")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }

}